﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace BrettM.Helpers
{
    public static class StringExtensions
    {

        public static string RemoveHtml(this string value)
        {
            if (value == null) return null;

            return Regex.Replace(value, @"<(.|\n)*?>", string.Empty);
        }


        public static string RemoveWhitespace(this string value)
        {
            if (value == null) return null;
            var result = new Regex(@"\s+").Replace(value, " ");
            return result.Trim();
        }


        public static string GetFirstWords(this string value, int numberOfWords)
        {
            var words = value.Replace("  ", " ");
            var wordCount = 0;
            var pos = 0;

            while (wordCount < numberOfWords && pos < words.Length)
            {
                if (words[pos++] == ' ')
                    wordCount++;
            }

            return words.Substring(0, pos).Trim();
        }

        internal static string[] ParseRowAsCsv(this string row)
        {
            if (row == null) return null;

            var cols = new List<string>();

            var inQuote = false;
            var quoteEnded = false;
            var word = string.Empty;

            for (var i = 0; i < row.Length; i++)
            {
                var ch = row[i];

                switch (ch)
                {
                    case '\t':
                    case ' ':
                        if (inQuote || word.Length>0) goto default;
                        break;

                    case '"':
                        {
                            bool isDoubleQuote = ((i + 1) < row.Length && row[i + 1] == '"');
                            if (inQuote)
                            {
                                if (isDoubleQuote)
                                {
                                    word += "\"";
                                    i++;
                                    break;
                                }
                                inQuote = false;
                                quoteEnded = true;
                                break;
                            }                            
                            if (isDoubleQuote)
                            {
                                word += "\"";
                                i++;
                                if (((i + 1) < row.Length && row[i + 1] == '"'))
                                {
                                    i++;
                                    inQuote = true;
                                }
                                break;                                
                            }
                            if (word.Length > 0) throw new InvalidDataException((i+1).ToString()); 
                            inQuote = true;
                        }
                        break;

                    case ',':
                        if (inQuote) goto default;
                        
                        cols.Add(word.Trim());
                        word = string.Empty;
                        quoteEnded = false;
                        break;

                    default:
                        if (!inQuote && quoteEnded && (ch != ' ' && ch != '\t')) throw new InvalidDataException((i + 1).ToString()); 
                        word += ch;
                        break;
                }
            }
            if (inQuote) throw new InvalidDataException((row.Length).ToString()); 
            if (word.Trim() != string.Empty)
                cols.Add(word.Trim());


            return cols.ToArray();
        }
    }
}
